package com.example.himalaya.view.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class TrackListAdapter extends RecyclerView.Adapter<TrackListAdapter.InnerHolder> {

    public static final String TAG = "TrackListAdapter";
    private List<Track> mDetailData = new ArrayList<>();

    // 格式化时间
    @SuppressLint("SimpleDateFormat")
    private SimpleDateFormat mUpdateDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @SuppressLint("SimpleDateFormat")
    private SimpleDateFormat mDurationFormat = new SimpleDateFormat("mm:ss");

    // Listener
    private ItemClickListener mItemClickListener = null;
    private ItemLongClickListener mItemLongClickListener;
    private View mItemView;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_track, parent, false);

        return new InnerHolder(itemView);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {

        // get element
        mItemView = holder.itemView;
        // 顺序id
        TextView orderTv = mItemView.findViewById(R.id.detail_order_text);
        // title
        TextView titleTv = mItemView.findViewById(R.id.detail_item_title);
        // 播放次数
        TextView playCountTv = mItemView.findViewById(R.id.detail_item_play_count);
        // 时长
        TextView durationTv = mItemView.findViewById(R.id.detail_item_duration);
        // 更新日期
        TextView updateDateTv = mItemView.findViewById(R.id.detail_item_time);

        // 设置数据

        Track track = mDetailData.get(position);

        orderTv.setText((position + 1) + "");
        titleTv.setText(track.getTrackTitle());
        playCountTv.setText(track.getPlayCount() + "");

        // 将官方给的秒的时间转化为毫秒
        int durationMil = track.getDuration() * 1000;
        // 将毫秒转化为mm-ss（分钟-秒）的格式
        String durarion = mDurationFormat.format(durationMil);

        durationTv.setText(durarion);
        // 将官方个的日期数据转为(yyyy-MM-dd) 年-月-日 的格式
//        String updateTimeText = mUpdateDateFormat.format(track.getUpdatedAt());
//        updateDateTv.setText(updateTimeText);
        updateDateTv.setText(track.getAnnouncer().getNickname());


        // 设置点击事件
        mItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(mDetailData, position);
                }
            }
        });

        mItemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                if (mItemLongClickListener != null) {
                    mItemLongClickListener.onItemLongClick(mDetailData.get(position));
                }

                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDetailData.size();
    }

    public void setData(List<Track> tracks) {
        // 清除老数据
        mDetailData.clear();
        // 设置新数据
        mDetailData.addAll(tracks);

        LogUtil.d(TAG, "data size -->" + mDetailData.size());

        // 更新
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface ItemClickListener {
        void onItemClick(List<Track> detailData, int position);
    }

    public void setItemLongClickListener(ItemLongClickListener listener) {
        this.mItemLongClickListener = listener;
    }

    public interface ItemLongClickListener {
        void onItemLongClick(Track track);
    }
}
